//
//  SettingContentView.swift
//  设置 - 内容
//
//  Created by 千策万划-mini on 2025/4/29.
//

import Foundation

class SettingContentView: UIView {
    
    var blockLogoutClick:(() -> Void)?
    var blockCancelAccountClick:(() -> Void)?
    
    // MARK: - lazy
    lazy var imageLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(148), W(149))
        result.image = UIImage(named: "icon_setting_logo")
        return result
    }()
    lazy var viewVersionBg: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(355), W(68))
        result.setBackgroundImage(UIImage(named: "icon_settings_item_bg"), for: .normal)
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelVersionTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Version",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "2D0084"),
            outlineColor: UIColor(hex: "2D0084")
        )
        return result
    }()
    lazy var labelVersion: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: GlobalMethod.getVersion ?? "",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "2D0084"),
            outlineColor: UIColor(hex: "2D0084")
        )
        return result
    }()
    lazy var viewLogoutBg: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(355), W(68))
        result.setBackgroundImage(UIImage(named: "icon_settings_item_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelLogoutTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Logout",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "2D0084"),
            outlineColor: UIColor(hex: "2D0084")
        )
        return result
    }()
    lazy var imageLogout: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_arrow_detail")
        result.widthHeight = XY(24, 24)
        return result
    }()
    lazy var btnCancelAccout: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(284), W(58))
        result.setBackgroundImage(UIImage(named: "icon_setting_cancel_account"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelCancelAccout: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Cancel Account",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "2D0084"),
            outlineColor: UIColor(hex: "2D0084")
        )
        result.isUserInteractionEnabled = false
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, 0)
        self.backgroundColor = .clear
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageLogo)
        addSubview(viewVersionBg)
        viewVersionBg.addSubview(labelVersionTitle)
        viewVersionBg.addSubview(labelVersion)
        addSubview(viewLogoutBg)
        viewLogoutBg.addSubview(labelLogoutTitle)
        viewLogoutBg.addSubview(imageLogout)
        addSubview(btnCancelAccout)
        btnCancelAccout.addSubview(labelCancelAccout)
        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            if let block = blockLogoutClick {
                block()
            }
            break
        case 2:
            if let block = blockCancelAccountClick {
                block()
            }
            break
        case 3:
            let vsrsion = GlobalMethod.getVersion ?? ""
            GlobalMethod.showAlert(strAlert: "Version：\(vsrsion)")
            break
        default:
            break
        }
    }
    
    
    // MARK: - 重置界面
    func resetView() {
        imageLogo.centerXTop = XY(width/2, 23)
        
        viewVersionBg.centerXTop = XY(width/2, imageLogo.bottom+21)
        labelVersionTitle.leftCenterY = XY(23, viewVersionBg.height/2-2)
        labelVersion.rightCenterY = XY(viewVersionBg.width-labelVersionTitle.left, labelVersionTitle.centerY)
        
        viewLogoutBg.centerXTop = XY(width/2, viewVersionBg.bottom+16)
        labelLogoutTitle.leftCenterY = XY(labelVersionTitle.left, viewLogoutBg.height/2-2)
        imageLogout.rightCenterY = XY(labelVersion.right, labelLogoutTitle.centerY)
        
        btnCancelAccout.centerXTop = XY(width/2, viewLogoutBg.bottom+30)
        labelCancelAccout.centerXCenterY = XY(btnCancelAccout.width/2, btnCancelAccout.height/2)
        
        height = btnCancelAccout.bottom + 60
    }
}
